package uk.co.cocking.getinline2.builder;

import org.hamcrest.Matcher;

import uk.co.cocking.getinline2.matchers.FieldMatcher;
import uk.co.cocking.getinline2.pipeline.transformers.CsvFormatter;
import uk.co.cocking.getinline2.pipeline.transformers.FieldExtractor;
import uk.co.cocking.getinline2.pipeline.transformers.Filter;
import uk.co.cocking.getinline2.pipeline.transformers.Transformer;
import uk.co.cocking.getinline2.pipeline.transformers.XmlFormatter;
import uk.co.cocking.getinline2.record.Record;

@SuppressWarnings("unchecked")
public class RecordConsumerBuilder<OriginalType, IncomingType, OutgoingType> extends Builder<OriginalType, IncomingType, OutgoingType> {
	
	public RecordConsumerBuilder(Transformer<IncomingType, OutgoingType> transformer,
			Builder<OriginalType, ?, IncomingType> previousBuilder) {
		super(transformer, previousBuilder);
	}
	
	public StringConsumerBuilder<OriginalType, Record, String> formatAsCsv() {
		return new StringConsumerBuilder<OriginalType, Record, String>(
				new CsvFormatter(), 
				(Builder<OriginalType, ?, Record>) this);
	}

	public RecordConsumerBuilder<OriginalType, Record, Record> addUrlContent(String newContentFieldName, String urlFieldName) {
		return new RecordConsumerBuilder<OriginalType, Record, Record>(
				new UrlMerger(newContentFieldName, urlFieldName),
				(Builder<OriginalType, ?, Record>) this);
	}
	
	public StringConsumerBuilder<OriginalType, Record, String> fieldExtractor(
			String fieldName) {
		return new StringConsumerBuilder<OriginalType, Record, String>(
				new FieldExtractor(fieldName),
				(Builder<OriginalType, ?, Record>) this);
	}

	public RecordConsumerBuilder<OriginalType, Record, Record> selectIfField(
			String fieldName, 
			Matcher<? super String> matcher) {
		return new RecordConsumerBuilder<OriginalType, Record, Record>(
				new Filter<Record>(matchedField(fieldName, matcher)),			
						(Builder<OriginalType, ?, Record>) this);
	}

	public StringConsumerBuilder<OriginalType, Record, String> formatAsXml() {
		return new StringConsumerBuilder<OriginalType, Record, String>(
				new XmlFormatter(), 
				(Builder<OriginalType, ?, Record>) this);
	}

	public static FieldMatcher matchedField(String fieldName,
			Matcher<? super String> matcher) {
		return new FieldMatcher(fieldName, matcher);
	}

}
